<template>
  <div class="app-container">
    <h2 style="text-align: center">发布新课程</h2>
    <el-steps
      :active="2"
      align-center
      style="margin-bottom: 40px"
      process-status="wait"
    >
      <el-step title="填写课程基本信息"></el-step>
      <el-step title="创建课程大纲"></el-step>
      <el-step title="发布课程"></el-step>
    </el-steps>
    <el-button type="text" @click="dialogVisible=true">添加章节</el-button>
 <el-dialog title="添加分类" :visible.sync="dialogVisible">
      <el-form :model="chapterList"  ref='chapterForm'   :rules="rules">
        <el-form-item label="章节标题" prop='name'>
          <el-input v-model="chapterList.title" />
        </el-form-item>
        <el-form-item label="课程排序" prop='sort'>
         <el-input-number v-model="chapterList.sort" controls-position="right" :min="0"></el-input-number>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onsubmit">确 定</el-button>
      </span>
    </el-dialog>
    <ul class="chanpterList">
      <li v-for="chapter in chapterNestedList" :key="chapter.id">
        <p>
          {{chapter.title}}
          <span class="acts">
            <el-button type="text" @click="addVideo(chapter.id)">添加课时</el-button>
            <el-button type="text" @click="getList(chapter.id)">编辑</el-button>
            <el-button type="text" @click="delList(chapter.id)">删除</el-button>
          </span>
        </p>
        <ul class="chanpterList videoList">
          <li v-for="video in chapter.children" :key="video.id">
            <p>
            {{video.title}}
              <span class="acts">
                <el-button type="text" @click="open(video.id)">编辑</el-button>
                <el-button type="text" @click="delVideo(video.id)">删除</el-button>
              </span>
            </p>
          </li>
        </ul>
      </li>
    </ul>
          <el-dialog title="添加分类" :visible.sync="dialogVideo">
      <el-form :model="chapterList" ref='videoForm' :rules="rulesVideo">
        <el-form-item label="章节标题" prop='name'>
          <el-input v-model="videoList.title" />
        </el-form-item>
        <el-form-item label="课程排序" prop='sort'>
         <el-input-number v-model="videoList.sort" controls-position="right" :min="0"></el-input-number>
        </el-form-item>
        <el-form-item label="收费情况">
          <el-radio v-model="videoList.isFree" :label="1">免费</el-radio>
          <el-radio v-model="videoList.isFree" :label="0">默认</el-radio>
       </el-form-item>
  <el-form-item label="上传视频">
    <el-button type="primary" size='mini'>上传视频</el-button>
    <el-tooltip class="item" effect="dark" placement="right">
      <div slot="content">多行信息<br/>第二行信息</div>
      <i class="el-icon-question"></i>
    </el-tooltip>
  </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVideo = false">取 消</el-button>
        <el-button type="primary" @click="onVideo">确 定</el-button>
      </span>
    </el-dialog>
    <!-- <el-button type="primary" >保存并下一步</el-button> -->
  </div>
</template>
<script>
import chapterAPI from "@/api/chapter";
import videoApi from '@/api/video'
export default {
  data() {
    return {
      couresId: "",//课程id
      chapterNestedList:[],//课程大纲列表
      dialogVisible:false, //弹框
      dialogVideo:false, //小节的弹框
      chapterList:{
        courseId:'',
        title:'',
        sort:0
        
      }, //像后端传递的数据
      videoList:{
        courseId: "",
        chapterId: "",
        title: "",
        videoSourceId: "",
        videoOriginalName: "",
        sort: 0,
        isFree: 0,
      },
       rules: {
          title: [
            { required: true, message: '请输入课程章节', trigger: 'blur' },
            { max: 50, message: '长度最大为50个字符', trigger: 'blur' }
          ],
          sort: [
            { type:"number", message: '排序必须是数字', trigger: 'blur' },
            { required: true, message: '请输入序号', trigger: 'blur' }
          ],},
          rulesVideo: {
          title: [
            { required: true, message: '请输入课程章节', trigger: 'blur' },
            { max: 50, message: '长度最大为50个字符', trigger: 'blur' }
          ],
          sort: [
            { type:"number", message: '排序必须是数字', trigger: 'blur' },
            { required: true, message: '请输入序号', trigger: 'blur' }
          ],}
    }
  },
  created() {
    this.init();
  },
  methods: {
    // 删除小节
    delVideo(videoId){
      // 弹出对话框是否删除
      this.$confirm("您确定要删除该章节吗?", "温馨提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(()=>{
        videoApi.delList(videoId).then(res=>{
          this.$message({
              type:'success',
              message:'删除成功'
          })
          this.init()
        })
      })
    },
    // 删除章节
    delList(id){
      // 弹出对话框是否删除
      this.$confirm("您确定要删除该章节吗?", "温馨提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(()=>{
        console.log(id);
        chapterAPI.delList(id).then(res=>{
          this.$message({
              type:'success',
              message:'删除成功'
          })
          this.init()
        })
      })
    },
    // 添加小节课程
    addVideo(chapterId){
      console.log(chapterId);
      this.dialogVideo = true
      // debugger
      //  清空表单数据
      this.videoList = {
        id:"",
        courseId:"",
        chapterId:chapterId,
        title: "",
        videoSourceId: "",
        videoOriginalName: "",
        sort: 0,
        isFree: 0,
      };

    },
    init() {
      if (this.$route.params && this.$route.params.id) {
        // 执行更新操作
        // 根据课程id获取当前更新课程信息
        this.couresId = this.$route.params.id;
        // console.log(couresId)
        this.getChapterTree(this.couresId);
      } else {
      }
    },
    // 获取课程大纲
    getChapterTree(couresId) {
      chapterAPI.getTreeList(couresId).then((res) => {
        // console.log(res);
        this.chapterNestedList=res.data
      });
    },
    // 添加课程
    onsubmit(){
      this.$refs['chapterForm'].validate((valid) => { 
     if (valid) {
         // 判断执行的是更新还是添加    
      if(this.chapterList.id){
        // console.log("执行编辑")
        this.updateChapter()
      }else{
        // console.log("执行添加")
        this.addChapter()
      }
     }

    });
    },
    // 添加课程
    addChapter(){
      this.chapterList.courseId = this.couresId
        chapterAPI.addList(this.chapterList).then(res=>{
            this.$message({
              type:'success',
              message:'添加成功'
            })
            this.dialogVisible = false;
            // 初始化数据
        this.init()
        })
        
    },
    // 添加小结
    onVideo(){
     this.$refs['videoForm'].validate((valid) => { 
       console.log(valid);
     if (valid) {
       console.log("这是",this.chapterList);
         // 判断执行的是更新还是添加    
      if(this.videoList.id){
        // console.log("执行编辑")
        this.updateVideo()
      }else{
        // console.log("执行添加")
        this.videoAdd()
      }
     }

    });
    },
    // 添加课程小结
    videoAdd(){
       // 为课程id赋值
      this.videoList.courseId = this.couresId;
      console.log(this.videoList);
      videoApi.addList(this.videoList).then(res=>{
        this.$message({
          type:'success',
          message:'添加成功!!!!'
        })
        this.dialogVideo = false;
        this.init()
      })
    },
    getList(id){
      this.dialogVisible = true;
      chapterAPI.getList(id).then(res=>{
        this.chapterList=res.data
      })
    },
    // 修改数据
    updateChapter(){
      chapterAPI.updateList(this.chapterList).then(res=>{
        // console.log(res);
        this.$message({
          type:'success',
          message:'更新成功'
        })
            this.dialogVisible = false;
            // 初始化数据
        this.init();
        })
      },
      open(id){
        this.dialogVideo = true;
        videoApi.getList(id).then(res=>{
          this.videoList = res.data
        })
      },
      // 修改小节数据
      updateVideo(){
        videoApi.updateList(this.videoList).then(res=>{
          this.$message({
            type:'success',
            message:'修改成功'
          })
          this.dialogVideo = false
          this.init()
        })
      }
  },
};
</script>
<style scoped>
.chanpterList {
  position: relative;
  list-style: none;
  margin: 0;
  padding: 0;
}
.chanpterList li {
  position: relative;
}
.chanpterList p {
  float: left;
  font-size: 20px;
  margin: 10px 0;
  padding: 10px;
  height: 70px;
  line-height: 50px;
  width: 100%;
  border: 1px solid #ddd;
}
.chanpterList .acts {
  float: right;
  font-size: 14px;
}

.videoList {
  padding-left: 50px;
}
.videoList p {
  float: left;
  font-size: 14px;
  margin: 10px 0;
  padding: 10px;
  height: 50px;
  line-height: 30px;
  width: 100%;
  border: 1px dotted #ddd;
}
</style>